package com.dhl.fin.api.timer;

import com.dhl.fin.api.common.annotation.SchedulePlan;
import com.dhl.fin.api.common.service.ExceptionRecordService;
import com.dhl.fin.api.dto.TipMessageDTO;
import com.dhl.fin.api.service.AbilityServiceCenterService;
import com.dhl.fin.api.service.LoginAccountService;
import com.dhl.fin.api.service.messageService.TipWillStopMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author CuiJianbo
 * @since 2025/6/14
 */
@Service
public class RemindTimer {

    @Autowired
    private AbilityServiceCenterService abilityServiceCenterService;

    @Autowired
    private LoginAccountService loginAccountService;

    @Autowired
    private ExceptionRecordService exceptionRecordService;


    @SchedulePlan(name = "检查哪些账号的订阅次数不足", cron = "0 0 8 * * *", persistException = true, env = "prod,uat")
    public void calculateMonthInterest() {
        loginAccountService.getAccountsOfWillStop().stream()
                .forEach(item -> {
                    TipWillStopMessage message = new TipWillStopMessage(item);
                    TipMessageDTO tipMessageDTO = message.tipMessage();
                    tipMessageDTO.setOpenId(item.getOpenIdd());
                    try {
                        abilityServiceCenterService.sendSubscribeMsgMiniProgram(tipMessageDTO);
                    } catch (Exception e) {
                        if ("给微信用户发送提醒消息失败".equals(e.getMessage())) {
                            loginAccountService.subAllowCount(item.getOpenIdd());
                        }
                        return;
                    }
                    loginAccountService.subAllowCount(item.getOpenIdd());
                });
    }

}


